﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WebForm
{
    public partial class GioHang : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            long tong = 0;
            if (Session["cart"] != null)
            {
                DataTable table = (DataTable)Session["cart"];
                grdCart.DataSource = table;
                grdCart.DataBind();

                
                foreach (DataRow r in table.Rows)
                {
                    tong += Int32.Parse(r["thanhtien"].ToString());
                }
            }
            label_tongtien.Text = "<td><span class='grandtotal'>" + tong + " đ</span></td>";
        }

        protected void Button_Tieptuc_Click(Object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx");
        }
        protected void Button_Thanhtoan_Click(Object sender, EventArgs e)
        {
            if (Session["cart"] == null)
            {
                return;
            }
            if (((DataTable)Session["cart"]).Rows.Count == 0)
            {
                return;
            }
            Response.Redirect("ThanhToan.aspx");
        }
        protected void grdCart_ItemCommand1(object source, DataGridCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                DataTable dt = (DataTable)Session["cart"];
                int chiso = int.Parse(e.CommandArgument.ToString());
                string str = e.CommandArgument.ToString();
                try
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if (dt.Rows[i]["id"].ToString().Equals(str))
                        {
                            dt.Rows.RemoveAt(i);
                            Session["cart"] = dt;
                            Response.Redirect("GioHang.aspx");
                        }
                    }
                }
                catch
                {
                    Response.Redirect("GioHang.aspx");
                }
            }
        }
    }
}